#include "TextureUtils.h"

#include "Model3D.h"
#include "Triangle3D.h"
#include "Point3D.h"

namespace Textures
  {

  void ApplyTextures(Model3D& io_model)
    {
    double min_x, max_x, min_y, max_y;
    min_x = max_x = io_model.Points().begin()->X();
    min_y = max_y = io_model.Points().begin()->Y();
    for (auto it =  io_model.Points().begin(); it != io_model.Points().end(); ++it)
      {
      min_x = std::min(min_x, it->X());
      max_x = std::max(max_x, it->X());
      min_y = std::min(min_y, it->Y());
      max_y = std::max(max_y, it->Y());
      }

    for(auto iterator = io_model.TrianglesMutable().begin(); iterator != io_model.TrianglesMutable().end(); iterator++)
      {
      Triangle3D& triangle = *iterator;
      TVector2 points[3];
      for (size_t i = 0; i < 3; ++i)
        {
        points[i] = TVector2((triangle.GetPoint(i).X() - min_x) / (max_x - min_x), (triangle.GetPoint(i).Y() - min_y) / (max_y - min_y));
        }
      triangle.SetPointsUV(points[0], points[1], points[2]);
      }
    }

  }
